package com.atguigu.tingshu.account.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.atguigu.tingshu.account.mapper.RechargeInfoMapper;
import com.atguigu.tingshu.account.mapper.UserAccountDetailMapper;
import com.atguigu.tingshu.account.mapper.UserAccountMapper;
import com.atguigu.tingshu.account.service.RechargeInfoService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.atguigu.tingshu.vo.account.RechargeInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
@SuppressWarnings({"all"})
public class RechargeInfoServiceImpl extends ServiceImpl<RechargeInfoMapper, RechargeInfo> implements RechargeInfoService {

	@Autowired
	private RechargeInfoMapper rechargeInfoMapper;
    @Autowired
    private UserAccountMapper userAccountMapper;

    @Autowired
    private UserAccountDetailMapper userAccountDetailMapper;

    /**
     * 充值余额
     * @param userId
     * @param rechargeInfoVo
     * @return
     */
    @Override
    public Map<String, String> submitRecharge(Long userId, RechargeInfoVo rechargeInfoVo) {
        //保存充值信息
        RechargeInfo rechargeInfo = new RechargeInfo();
        rechargeInfo.setUserId(userId);
        rechargeInfo.setOrderNo(DateUtil.today().replace("-", "") + IdUtil.getSnowflakeNextIdStr());
        rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_UNPAID);
        rechargeInfo.setRechargeAmount(rechargeInfoVo.getAmount());
        rechargeInfo.setPayWay(rechargeInfoVo.getPayWay());
        rechargeInfoMapper.insert(rechargeInfo);
        Map<String, String> mapResult = new HashMap<>();
        mapResult.put("orderNo", rechargeInfo.getOrderNo());
        return mapResult;
    }

    /**
     * 用户充值，支付成功后，充值业务处理
     * @param orderNo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void rechargePaySuccess(String orderNo) {
        RechargeInfo rechargeInfo = rechargeInfoMapper.selectOne(new QueryWrapper<RechargeInfo>().eq("order_no", orderNo));
        if (rechargeInfo != null && SystemConstant.ORDER_STATUS_PAID.equals(rechargeInfo.getRechargeStatus())) {
            return;
        }
        //修改用户账户金额
        BigDecimal rechargeAmount = rechargeInfo.getRechargeAmount();
        int result = userAccountMapper.addAmount(rechargeInfo.getUserId(), rechargeAmount);
        if (result <= 0){
            throw new GuiguException(400, "充值失败");
        }
        //增加明细
        UserAccountDetail userAccountDetail = new UserAccountDetail();
        userAccountDetail.setUserId(rechargeInfo.getUserId());
        userAccountDetail.setTitle("充值：" + rechargeAmount.intValue());
        userAccountDetail.setTradeType(SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT);
        userAccountDetail.setAmount(rechargeAmount);
        userAccountDetail.setOrderNo(orderNo);
        userAccountDetailMapper.insert(userAccountDetail);
        //修改支付信息
        rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_PAID);
        rechargeInfoMapper.updateById(rechargeInfo);
    }

    /**
     * 根据订单号获取充值信息
     * @param orderNo
     * @return
     */
    @Override
    public RechargeInfo getRechargeInfo(String orderNo) {
        RechargeInfo rechargeInfo = rechargeInfoMapper.selectOne(new QueryWrapper<RechargeInfo>().eq("order_no", orderNo));
        return rechargeInfo;
    }
}
